import { createRouter, createWebHashHistory } from 'vue-router'

import Layout from '@/layout/Index.vue'

import store from '@/store'
import { menuTree } from '@/apis/personal'

const Home = () => import('../views/Home.vue')
const Register = () => import("../views/Register.vue")

//页面
const Login = () => import("@/views/login/Index.vue")
const PersonalIndex = () => import("@/views/personal/Index.vue")
const PersonalMessage = () => import("@/views/personal/Message.vue")
const AppIndex = () => import("@/views/app/Index.vue")

const NotFount = () => import("@/views/404.vue")


const routers = [
  {
    path: '/',
    name: 'Hone',
    component: Layout,
    children: [
      {
        path: 'login',
        name: 'Login',
        component: Login
      },
      {
        path: 'personal',
        name: 'Personal',
        component: PersonalIndex,
        children: [
          {
            path: 'message',
            name: 'PersonalMessage',
            component: PersonalMessage
          },
        ]
      },

      {
        path: '404',
        name: 'NotFound',
        component: NotFount
      }
    ],
  },
  {
    path: '/:pathMatch(.*)*',
    name: '404',
    redirect: '/404'
  }
  // ,
  // {
  //   path: '/home',
  //   name: 'home',
  //   component: Home
  // },
  // {
  //   path: '/register',
  //   name: 'register',
  //   component: Register
  // },



]


// const router = createRouter({
//   history: createWebHashHistory(),
//   routes: routeConfig
// })

//应用管理
const AppDept = () => import("@/views/app/Dept.vue")
const AppResoure = () => import("@/views/app/Resoure.vue")
const AppROle = () => import("@/views/app/ROle.vue")
const AppUser = () => import("@/views/app/User.vue")

//应用管理的路由
{
  path: '/app',
    nmae: 'App',
      children: [
        {
          path: 'user',
          name: 'AppUser',
          component: AppUser
        },
        {
          path: 'dept',
          name: 'AppDept',
          component: AppDept
        },
        {
          path: 'role',
          name: 'AppRole',
          component: AppRole
        },
        {
          path: 'resource',
          name: 'AppResource',
          component: AppResource
        },
      ]

}


//系统管理
const SysUser = () => import("@/views/sys/User.vue")
const SysNotice = () => import("@/views/sys/Notice.vue")

//系统管理的路由
{
  path: '/sys',
    nmae: 'Sys',
      children: [
        {
          path: 'user',
          name: 'SysUser',
          component: SysUser
        },
        {
          path: 'notice',
          name: 'SysNotice',
          component: SysNotice
        }
      ]

}


//审计管理
const LogsVisit = () => import("@/views/logs/Visit.vue")
const LogsOperation = () => import("@/views/logs/Operation.vue")

//系统管理的路由
{
  path: '/logs',
    nmae: 'Logs',
      children: [
        {
          path: 'visit',
          name: 'LogsVisit',
          component: LogsVisit
        },
        {
          path: 'operation',
          name: 'LogsOperation',
          component: LogsOperation
        }
      ]
}

// 1.如果是去登录页而且已经登陆，则直接跳转第一个导航
// 2.如果是首页，则直接跳转到第一个导航
router.beforeEach(async (to) => {
  const isLogin = store.getters['user/isLogin']

  // 1.如果是去登录页而且已经登陆，则直接跳转第一个导航
  if (to.path == '/login') {
    if (isLogin) {
      console.log(store.state.firstRoute);
      return store.state.firstRoute
    }
    return true
  }

  //请求菜单数据
  await intMenu()
  const { state: { firstRoute } } = store
  // 2.如果是首页，则直接跳转到第一个导航
  if (to.path === '/' && firstRoute) {
    return firstRoute
  }
  return true

})

function intMenu() {
  return menuTree()
    .then((res) => {
      const { data } = res

      //菜单/路由已经加载
      store.commit('setRouteLoaded', true)

      //设置菜单数据
      store.commit('setmenuTree', data)

      //设置访问首页时默认访问的页面
      store.commit('setFistRoute', data[0].children[0].path)
    })
}




export default router
